
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>订单管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">转账订单</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:left; margin-bottom: 10px;">
              <div class="layui-form-item" style="margin:0;">
                <div class="layui-input-inline">
                  <input type="text" name="createTimeStart" id="createTimeStart" autocomplete="off" placeholder="开始时间" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="createTimeEnd" id="createTimeEnd" autocomplete="off" placeholder="结束时间" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <select name="mchId" id="mchId" lay-search="">
                    <option value="-99">商户信息</option>
                  </select>
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="transOrderId" id="transOrderId" placeholder="转账订单号" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="mchTransNo" id="mchTransNo" placeholder="系统单号" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <input type="text" name="appId" id="appId" placeholder="商户单号" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                  <select name="passageId" id="passageId" lay-search="">
                    <option value="">代付渠道</option>
                  </select>
                </div>
                <div class="layui-input-inline">
                  <select name="type" id="status" lay-search="">
                    <option value="-99">转账状态</option>
                    <option value="0">订单生成</option>
                    <option value="1">转账中</option>
                    <option value="2">转账成功</option>
                    <option value="3">转账失败</option>
                    <option value="4">处理完成</option>
                  </select>
                </div>
                <button id="search" class="layui-btn" data-type="reload">搜索</button>
              </div>
            </div>
            <div class="layui-form-item">
              <blockquote class="layui-elem-quote" id="amountTip">
                提交订单数:<sapn id="allTotalCount" style="color: blue; margin-right: 10px;"></sapn>
                订单总金额:<span id="allTotalAmount" style="color: blue; margin-right: 10px;"></span>
                成功订单数:<span id="successTotalCount" style="color: green; margin-right: 10px;"></span>
                成功总金额:<span id="successTotalAmount" style="color: green; margin-right: 10px;"></span>
                渠道总成本:<span id="successTotalChannelCost" style="color: green; margin-right: 10px;"></span>
                成功率:<span id="sucRate" style="color: green; margin-right: 10px;"></span>
                商户代付余额:<span id="mchDfBalance" style="color: green; margin-right: 10px;"></span>
              </blockquote>
            </div>
          </div>
<!--          <button class="layui-btn" id="batchDeal" data-type="reload">批量处理</button>-->
          <table id="XxPay_Mgr_TransOrder_dataAll" lay-filter="XxPay_Mgr_TransOrder_dataAll"></table>
        </div>

      </div>
    </div>
  </div>
</div>
<form class="layui-form" id="test" style="display:none">
  <div class="layui-form-item">
    <label class="layui-form-label">状态</label>
    <div class="layui-input-inline" >
      <input type="radio" name="status" title="成功" value="2" checked/>
      <input type="radio" name="status" title="失败" value="3" />
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">备注</label>
    <div class="layui-input-inline">
      <input type="text"  id="remark"  name="remark"  placeholder="请输入备注" autocomplete="off" class="layui-input ">
    </div>
  </div>
<!--  <div class="layui-form-item">-->
<!--    <label class="layui-form-label">超级密码</label>-->
<!--    <div class="layui-input-inline">-->
<!--      <input type="password" required id="superpassword"  name="superpassword"  lay-verify="required|pass" placeholder="请输入超级密码" autocomplete="off" class="layui-input pwd">-->
<!--    </div>-->
<!--  </div>-->
</form>

<script type="text/html" id="xxpayBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
<!--  {{#  if(d.status == 1){ }}-->
<!--  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="deal">处理</a>-->
<!--  {{#  } else { }}-->
<!--  <a class="layui-btn layui-btn-primary layui-btn-xs layui-btn-disabled" lay-event="deal">处理</a>-->
<!--  {{#  } }}-->
</script>

<script>
layui.use(['admin', 'table', 'util', 'laydate'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,table = layui.table
  ,element = layui.element
  ,laydate = layui.laydate
  ,form = layui.form;
  
  element.render('breadcrumb', 'breadcrumb');

  $.setCountResult = function(createTimeStart, createTimeEnd,mchId, transOrderId, mchTransNo,appId,status,passageId) {
    admin.req({
      type: 'get',
      url: layui.setter.baseUrl + '/trans_order/count',
      data: {
        mchId : mchId,
        transOrderId : transOrderId,
        mchTransNo : mchTransNo,
        createTimeStart : createTimeStart,
        createTimeEnd : createTimeEnd,
        appId: appId,
        status: status,
        passageId: passageId
      },
      error: function(err){
        layer.alert(JSON.stringify(err.field), {
          title: '错误提示'
        })
      },
      success: function(res){
        if(res.code == 0){
          $('#allTotalCount').html(res.data.allTotalCount);
          $('#allTotalAmount').html("￥" + res.data.allTotalAmount/100.00);
          $('#successTotalCount').html(res.data.successTotalCount);
          $('#successTotalAmount').html("￥" + res.data.successTotalAmount/100.00);
          $('#successTotalChannelCost').html("￥" + res.data.successTotalChannelCost/100.00);
          $('#sucRate').html((res.data.successTotalCount/res.data.allTotalCount * 100).toFixed(2) + "%");
          $('#mchDfBalance').html(res.data.totalAgnetBalacne/100.00);
        }
      }
    });
  };

  // 获取商户信息
  admin.req({
    type: 'post',
    url: layui.setter.baseUrl + '/mch_info/getAll',
    error: function(err){
      layer.alert(err);
    },
    success: function(res){
      if(res.code == 0){
        var mchtList = res.data;
        //遍历赋值
        for(var i in mchtList){
          $("#mchId").append('<option value= '+mchtList[i].mchId+'>'+mchtList[i].name+'</option>');
        }
        form.render('select');
      }
    }
  });


  // 获取代付通道信息
  admin.req({
    type: 'post',
    url: layui.setter.baseUrl + '/config/agentpay_passage/list',
    error: function(err){
      layer.alert(err);
    },
    success: function(res){
      if(res.code == 0){
        var channelList = res.data;
        //遍历赋值
        for(var i in channelList){
          $("#passageId").append('<option value= '+channelList[i].id+'>'+channelList[i].passageName+'</option>');
        }
        form.render('select');
      }
    }
  });

  var tplStatus = function(d){
    if(d.status == 0) {
      return "<span style='color: blue'>订单生成</span>";
    }else if(d.status == 1) {
      return "<span style='color: orangered'>转账中</span>";
    }else if(d.status == 2) {
      return "<span style='color: green'>转账成功</span>";
    }else if(d.status == 3) {
      return "<span style='color: red'>转账失败</span>";
    }else if(d.status == 4) {
      return "<span style='color: darkgreen'>处理完成</span>";
    }
  };

  var tplMchInfo = function(d){
    return "["+d.mchId+"]"+d.device;
  };

// 转账结果:0-不确认结果,1-等待手动处理,2-确认成功,3-确认失败
  var tplResult = function(d){
    if(d.result == 0) {
      return "<span style='color: blue'>不确认结果</span>";
    }else if(d.result == 1) {
      return "<span style='color: orangered'>等待处理</span>";
    }else if(d.result == 2) {
      return "<span style='color: green'>确认成功</span>";
    }else if(d.result == 3) {
      return "<span style='color: red'>确认失败</span>";
    }
  };

  var tplAgentpayChannel = function(d){
    if(d.passageId == 2000 || d.passageId == 4) {
      return '飞凡代付';
    }else if(d.passageId == 2002 || d.passageId == 6) {
      return '恒生支付';
    }else if(d.passageId == 2003 ) {
      return '合合支付';
    }else if(d.passageId == 2004 ) {
      return '星澜支付';
    }else if(d.passageId == 2005 ) {
      return '鸭头支付';
    }else if(d.passageId == 2006 ) {
      return '梦慈支付';
    }else{
      return '其他渠道';
    }
  };


  // 初始化统计结果
  var dateTime = new Date();
  dateTime.setDate(dateTime.getDate());
  dateTime = new Date(dateTime);
  var startTimeDefault = layui.util.toDateString(dateTime, "yyyy-MM-dd 00:00:00")
  // 初始化统计结果
  $.setCountResult(startTimeDefault);

  //用户列表
  table.render({
    elem: '#XxPay_Mgr_TransOrder_dataAll'
    ,url: layui.setter.baseUrl + '/trans_order/list' //用户列表接口
    ,where: {
        access_token: layui.data(layui.setter.tableName).access_token,
        createTimeStart: startTimeDefault
    }
    ,id: 'tableReload'
    ,page: true
    ,cols: [[
      // {type: 'checkbox', fixed: 'left'}
      {field: 'transOrderId', width: 220, title: '转账单号'}
      ,{field: 'mchTransNo', width: 220, title: '系统单号'}
      ,{field: 'appId', width: 220, title: '商户单号'}
      ,{field: 'mchInfo',width: 220,  title: '商户信息', templet: tplMchInfo}
      ,{field: 'agentpayChannel',width: 120,title: '代付渠道',templet: tplAgentpayChannel}
      // ,{field: 'device',width: 120, title: '商户名称'}
      ,{field: 'amount',width: 100, title: '转账金额', templet: '<div>{{ d.amount/100 }}</div>'}
      ,{field: 'status',width: 120, title: '转账状态', templet: tplStatus}
      ,{field: 'result',width: 120, title: '转账结果', templet: tplResult}
      ,{field: 'createTime', width: 150, title: '创建时间', templet: '<div>{{ layui.util.toDateString(d.createTime, "MM-dd HH:mm:ss") }}</div>'}
      ,{field: 'edit', width: 220, title: '操作', toolbar: '#xxpayBar' }
    ]]
    ,skin: 'line'
  });

  //监听工具条
  table.on('tool(XxPay_Mgr_TransOrder_dataAll)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){
      location.href = layui.setter.baseLocal + "order/trans/view.html?" + data.transOrderId;
    }else if(obj.event === 'deal') {
      var transOrderId = data.transOrderId;
      var load= layer.open({
        type:1,
        area:['500px','600px'],
        title: '订单处理'
        ,content: $("#test"),
        shade: 0,
        btn: ['确认']
        ,btn1: function(index, layero){
          var remark=$.trim($('#remark').val());
          // var superpassword=$.trim($('#superpassword').val());
          var data = {};
          data["status"] = $('input[type=radio][name=status]:checked').val();
          data["remark"] = remark;
          // data["superpassword"] = superpassword;
          data["transOrderId"] = transOrderId;
          admin.req({
            type: 'post',
            url: layui.setter.baseUrl + '/trans_order/deal',
            data: data,
            // data: {merchantIds : JSON.stringify(merchantIds)},
            error: function(err){
              layer.close(load);
              layer.alert(err);
            },
            success: function(res){
              layer.close(load);
              if(res.code == 0 ) {
                layer.alert("处理成功！",function(layid){
                  parent.layer.close(layid);
                  table.reload('tableReload');
                });
              }
            }
          });
        },
        cancel: function(layero,index){
          layer.closeAll();
        }
      });
    }
  });

  // $("#batchDeal").off().on("click", function(){
  //   var transOrderIds = [];
  //   var errorMsg = "";
  //   $.each(table.cache.tableReload, function(){
  //     var thisObject = $(".layui-table tr[data-index='"+this.LAY_TABLE_INDEX+"'] input[name='layTableCheckbox']");
  //     if(thisObject.is(':checked')){ //勾选状态
  //       transOrderIds.push(this.transOrderId);
  //     }
  //   });
  //   if(errorMsg) return layer.alert(errorMsg);
  //   if(transOrderIds.length <= 0) return layer.alert("请勾选需要处理的订单！");
  //   var load= layer.open({
  //     type:1,
  //     area:['500px','600px'],
  //     title: '订单批量处理'
  //     ,content: $("#test"),
  //     shade: 0,
  //     btn: ['提交']
  //     ,btn1: function(index, layero){
  //       var remark=$.trim($('#remark').val());
  //       var data = {};
  //       data["status"] = $('input[type=radio][name=status]:checked').val();
  //       data["remark"] = remark;
  //       data["transOrderIds"] = JSON.stringify(transOrderIds);
  //       admin.req({
  //         type: 'post',
  //         url: layui.setter.baseUrl + '/trans_order/batchDeal',
  //         data: data,
  //         error: function(err){
  //           layer.close(load);
  //           layer.alert(err);
  //         },
  //         success: function(res){
  //           layer.close(load);
  //           if(res.code == 0 ) {
  //             layer.alert("处理成功！",function(layid){
  //               parent.layer.close(layid);
  //               table.reload('tableReload');
  //             });
  //           }
  //         }
  //       });
  //     },
  //     cancel: function(layero,index){
  //       layer.closeAll();
  //     }
  //   });
  // });

  // 搜索
  var $ = layui.$, active = {
    reload: function(){
      var mchId = $('#mchId').val();
      var createTimeStart = $('#createTimeStart').val();
      var createTimeEnd = $('#createTimeEnd').val();
      var transOrderId = $('#transOrderId').val();
      var mchTransNo = $("#mchTransNo").val();
      var status = $("#status").val();
      var appId = $("#appId").val();
      var passageId = $("#passageId").val();
      $.setCountResult(createTimeStart, createTimeEnd,mchId, transOrderId, mchTransNo,appId,status,passageId);

      //执行重载
      table.reload('tableReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          mchId: mchId,
          transOrderId: transOrderId,
          mchTransNo: mchTransNo,
          status : status,
          createTimeStart : createTimeStart,
          createTimeEnd : createTimeEnd,
          appId: appId,
          passageId: passageId
        }
      });
    }
  };
  $('#search').on('click', function() {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  laydate.render({
    elem: '#createTimeStart'
    ,type: 'datetime'
    , value: startTimeDefault
    ,format: 'yyyy-MM-dd HH:mm:ss'
  });

  laydate.render({
    elem: '#createTimeEnd'
    ,type: 'datetime'
    ,format: 'yyyy-MM-dd HH:mm:ss'
  });
  // 渲染表单
  form.render();
});
</script>